home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Samples / SampleCode / Unsupported Libraries / PictRead.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-14  |  5.0 KB  |  223 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2.  **                                                                             **
  3.  **     Module:        PictRead.c                                                 **
  4.  **                                                                          **
  5.  **                                                                          **
  6.  **     Purpose:                                                               **
  7.  **                                                                          **
  8.  **                                                                          **
  9.  **                                                                          **
  10.  **     Copyright (C) 1992-1995 Apple Computer, Inc.  All rights reserved.     **
  11.  **                                                                          **
  12.  **                                                                          **
  13.  *****************************************************************************/
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. #include <StandardFile.h>
  19. #include <Memory.h>
  20.  
  21. #include <QDOffscreen.h>
  22. #include "QD3D.h"
  23. #include "QD3DStorage.h"
  24. #include "PictRead.h"
  25.  
  26.  
  27. /*===========================================================================*\
  28.  *
  29.  *    Routine:    OpenPICTFile()
  30.  *
  31.  *    Comments:    open a PICT file and read it into a PICT handle
  32.  *
  33. \*===========================================================================*/
  34.  
  35. PicHandle OpenPICTFile(
  36.     short     vRefNum, 
  37.     Str255     fName)
  38. {
  39.     short        fRefNum;
  40.     OSErr        err;
  41.     long        curEOF;
  42.     PicHandle    myPic;
  43.     long         count;
  44.     Ptr         buffer;
  45.     
  46.     /* open PICT file */
  47.     err = FSOpen(fName, vRefNum, &fRefNum);
  48.     if (err != 0) {
  49.         /*printf("Error - cannot open file\n"); */
  50.         exit(-1);
  51.     }
  52.     
  53.     /* get size of file */
  54.     err = GetEOF(fRefNum, &curEOF);
  55.     if (err != 0) {
  56.         /*printf("Error - cannot get EOF\n"); */
  57.         exit(-2);
  58.     }
  59.     
  60.     /* move the file mark to 512 */
  61.     err = SetFPos(fRefNum, fsFromStart, 512L);
  62.     if (err != 0) {
  63.         /*printf("Error - cannot seek 512\n"); */
  64.         exit(-3);
  65.     }
  66.  
  67.     /* size of data to read */
  68.     count = curEOF - 512;
  69.     
  70.     /* create the PicHandle */
  71.     myPic = (PicHandle)NewHandle(count);
  72.     HLock((Handle)myPic);
  73.     
  74.     /* read the PICT info */
  75.     buffer = (Ptr)(*myPic);
  76.     err = FSRead(fRefNum, &count, buffer);
  77.     if (err != 0) {
  78.         /*printf("Error - cannot read\n");*/
  79.         exit(-4);
  80.     }
  81.     HUnlock((Handle)myPic);
  82.     
  83.     /* release the file */
  84.     err = FSClose(fRefNum);
  85.     if (err != 0) {
  86.         /*printf("Error - cannot close file \n"); */
  87.         exit(-5);
  88.     }
  89.     
  90.     return (myPic);
  91. }
  92.  
  93.  
  94. /*===========================================================================*\
  95.  *
  96.  *    Routine:    GetPICTFile()
  97.  *
  98.  *    Comments:    Query user for PICT File
  99.  *
  100. \*===========================================================================*/
  101.  
  102. PicHandle GetPICTFile (
  103.     void)
  104. {
  105.     SFReply            reply;
  106.     static    Point    where = { 80, 80 };
  107.     PicHandle         picHandle;
  108.     SFTypeList        myTypes = { 'PICT' } ;
  109.     
  110.     SFGetFile(where, "\pChoose A PICT File", NULL, 1, myTypes, NULL, &reply);
  111.     
  112.     if (reply.good) {
  113.         picHandle = OpenPICTFile(reply.vRefNum, reply.fName);
  114.         return (picHandle);
  115.     }
  116.     
  117.     return (0);
  118. }
  119.  
  120.  
  121. /*===========================================================================*\
  122.  *
  123.  *    Routine:    LoadMapPICT()
  124.  *
  125.  *    Comments:    take a PICT handle and loads it into a bitmap structure
  126.  *
  127. \*===========================================================================*/
  128.  
  129. short LoadMapPICT(
  130.     PicHandle             pict,
  131.     unsigned long         mapID,
  132.     unsigned long         mapSizeX,
  133.     unsigned long         mapSizeY,
  134.     TQ3StoragePixmap     *bMap)
  135. {
  136.     unsigned long             *textureMap;
  137.     unsigned long            *textureMapAddr;
  138.     unsigned long             *pictMap;
  139.     unsigned long            pictMapAddr;
  140.     register unsigned long     row;
  141.     register unsigned long     col;
  142.     Rect                     rectGW;
  143.     GWorldPtr                 pGWorld;
  144.     PixMapHandle             hPixMap;
  145.     unsigned long             pictRowBytes;
  146.     QDErr                    err;
  147.     GDHandle                oldGD;
  148.     GWorldPtr                oldGW;
  149.     short                    success;
  150.     
  151.     (void)mapID;        /* unused argument */
  152.     
  153.     /* save current port */
  154.     GetGWorld(&oldGW, &oldGD);
  155.  
  156.     /* create the GWorld */
  157.     SetRect(&rectGW, 0, 0, (unsigned short)mapSizeX, (unsigned short)mapSizeY);
  158.  
  159.     err = NewGWorld(&pGWorld, 32, &rectGW, 0, 0, useTempMem);
  160.     if (err != noErr)
  161.         return 0;
  162.  
  163.     success = 1;
  164.     
  165.     hPixMap = GetGWorldPixMap(pGWorld);
  166.     pictMapAddr = (unsigned long)GetPixBaseAddr (hPixMap);
  167.     pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
  168.     
  169.     /* put the PICT into the window */
  170.     SetGWorld(pGWorld, nil);
  171.     
  172.     LockPixels(hPixMap);
  173.     EraseRect(&rectGW);
  174.     DrawPicture(pict, &rectGW);
  175.         
  176.     /* allocate an area of memory for the texture */
  177.     textureMap = (unsigned long *)malloc(mapSizeX * mapSizeY * sizeof(unsigned long));
  178.     if (textureMap == NULL) {
  179.         success = 0;
  180.         goto bail;
  181.     }
  182.     /* bMap->image = (char *)textureMap; */
  183.  
  184.     /* copy the PICT into the texture */
  185.     textureMapAddr = textureMap;
  186.     for (row = 0L; row < mapSizeY; row++) {
  187.         pictMap = (unsigned long *)(pictMapAddr + (pictRowBytes * row));
  188.         for (col = 0L; col < mapSizeX; col++) {
  189.             *textureMap++ = (*pictMap++ | 0xff000000L);
  190.         }
  191.     }
  192.         
  193.     bMap->image = Q3MemoryStorage_New((const unsigned char *)textureMapAddr, 
  194.                                   mapSizeX * mapSizeY * sizeof(unsigned long));
  195.                                   
  196.     if (bMap->image == NULL) {
  197.         /* error */
  198.         success = 0;
  199.         goto bail;
  200.     }
  201.  
  202.     UnlockPixels(hPixMap);
  203.     
  204.     bMap->width     = mapSizeX;
  205.     bMap->height    = mapSizeY;
  206.     bMap->rowBytes     = bMap->width * 4;
  207.     bMap->pixelSize = 32;
  208.     bMap->pixelType    = kQ3PixelTypeRGB32;
  209.     bMap->bitOrder    = kQ3EndianBig;
  210.     bMap->byteOrder    = kQ3EndianBig;
  211.     
  212.     /* Free junk */
  213. bail:
  214.  
  215.     SetGWorld(oldGW, oldGD);
  216.     
  217.     DisposeGWorld(pGWorld);
  218.     if (textureMapAddr != NULL)
  219.         free(textureMapAddr);
  220.     
  221.     return success;
  222. }
  223.